package com.sharer.last.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sharer.last.base.enums.BusinessExceptionCode;
import com.sharer.last.base.enums.ExceptionPrefixParamEnum;
import com.sharer.last.base.exception.BusinessException;
import com.sharer.last.base.model.PageVO;
import com.sharer.last.base.support.QueryCondition;
import com.sharer.last.base.utils.ConvertUtil;
import com.sharer.last.base.utils.StringUtil;
import com.sharer.last.model.dto.log.VisitLogChangeDTO;
import com.sharer.last.model.dto.log.VisitLogQueryDTO;
import com.sharer.last.model.vo.log.VisitLogVO;
import com.sharer.last.utils.BeanUtil;
import com.sharer.last.wrapper.VisitLogWrapper;
import org.springframework.stereotype.Service;
import com.sharer.last.model.po.VisitLogPO;
import com.sharer.last.mapper.VisitLogMapper;
import com.sharer.last.model.dto.UserAgentDTO;
import com.sharer.last.model.dto.VisitLogUuidTimeDTO;
import com.sharer.last.service.VisitLogService;
import com.sharer.last.utils.IpAddressUtils;
import com.sharer.last.utils.UserAgentUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 访问日志业务层实现
 *
 * @ClassName VisitLogServiceImpl
 * @Author wangjin
 * @Date 2023/5/5 20:46
 * @Description
 * @Version 1.0
 */
@Service
public class VisitLogServiceImpl extends ServiceImpl<VisitLogMapper, VisitLogPO> implements VisitLogService {
    @Resource
    VisitLogMapper visitLogMapper;

    @Resource
    VisitLogWrapper visitLogWrapper;

    @Resource
    UserAgentUtils userAgentUtils;

    /**
     * 分页查询
     *
     * @param queryDTO
     * @return
     */
    @Override
    public PageVO<VisitLogVO> selectListByPage(VisitLogQueryDTO queryDTO) {
        LambdaQueryWrapper<VisitLogPO> qw   = this.buildQueryWrapper(queryDTO);
        IPage<VisitLogPO>              page = this.page(QueryCondition.getPage(queryDTO.getPage()), qw);
        return visitLogWrapper.toPageVO(page);
    }

    private LambdaQueryWrapper<VisitLogPO> buildQueryWrapper(VisitLogQueryDTO queryDTO) {
        LambdaQueryWrapper<VisitLogPO> qw   = new LambdaQueryWrapper<>();
        String                         uuid = queryDTO.getUuid();
        if (StringUtil.isNotBlank(uuid)) {
            qw.eq(VisitLogPO::getUuid, uuid);
        }
        String startTime = queryDTO.getStartTime();
        if (startTime != null) {
            qw.gt(VisitLogPO::getCreateTime, startTime);
        }
        String endTime = queryDTO.getEndTime();
        if (endTime != null) {
            qw.lt(VisitLogPO::getCreateTime, endTime);
        }
        return qw;
    }

    /**
     * 创建访客日志
     *
     * @param dto
     */
    @Override
    public void createVisitLog(VisitLogChangeDTO dto) {
        dto.setId(null);
        VisitLogPO   po           = BeanUtil.copy(dto, VisitLogPO.class);
        String       ipSource     = IpAddressUtils.getCityInfo(dto.getIp());
        UserAgentDTO userAgentDTO = userAgentUtils.parseOsAndBrowser(dto.getUserAgent());
        po.setIpSource(ipSource);
        po.setOs(userAgentDTO.getOs());
        po.setBrowser(userAgentDTO.getBrowser());
        if (!this.save(po)) {
            throw new BusinessException(BusinessExceptionCode.DATA_REMOVE_FAIL, ExceptionPrefixParamEnum.VISITOR_LOG);
        }
    }

    /**
     * 删除访客日志
     *
     * @param id
     */
    @Override
    public void removeVisitLogById(Long id) {
        if (ConvertUtil.toInt(id) < 1) {
            throw new BusinessException(BusinessExceptionCode.ID_IS_NULL, ExceptionPrefixParamEnum.VISITOR_LOG);
        }
        // 查询数据是否存在
        VisitLogPO visitLogPO = this.getById(id);
        if (visitLogPO == null) {
            throw new BusinessException(BusinessExceptionCode.DATA_IS_EMPTY, ExceptionPrefixParamEnum.VISITOR_LOG);
        }
        if (!this.removeById(id)) {
            throw new BusinessException(BusinessExceptionCode.DATA_REMOVE_FAIL, ExceptionPrefixParamEnum.VISITOR_LOG);
        }
    }
}
